{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 第二章习题\n",
    "\n",
    "### 一、填空题\n",
    "\n",
    "1.列表的`sort()`方法没有返回值,或者说返回值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2.已知列表 `data=[1,2,30 4]`，那么`data[2:100]`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[30, 4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3.己知`x=3`和`y=5`,那么执行语句`x,y=y,x`之后，`y`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．已知字典 `data= {'a;97, 'A' :65}`,那么`data.get('a', None)`的值为________."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "97"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 二、判断题\n",
    "\n",
    "1.生成器表达式的计算结果是一个元组。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．包含列表的元组可以作为字典的“键”。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "错"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．包含列表的元组不可以作为集合的元素。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．列表的`rindex()`方法返回指定元素在列表中最后一次出现的位置。`(    )`"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 三、编程题\n",
    "\n",
    "1.输入一个字符串，输出其中每个字符的出现次数。要求使用标准库`collotections`中的`Counter`类,请自行查阅相关用法。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'831811463davdcxzeagad'\n",
      "Counter({'1': 3, 'd': 3, 'a': 3, '8': 2, '3': 2, '4': 1, '6': 1, 'v': 1, 'c': 1, 'x': 1, 'z': 1, 'e': 1, 'g': 1})\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "print(Counter(eval(input())))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2．输入一个字符串,输出其中只出现了一次的字符及其下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'cdsavsav84321c3d8zv44'\n",
      "[('2', 12), ('1', 13), ('z', 18)]\n"
     ]
    }
   ],
   "source": [
    "text = input()\n",
    "positions = [(ch,index) for index,ch in enumerate(text) if text.index(ch) == text.rindex(ch)]\n",
    "print(positions)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3．输入一个字符串,输出其中每个唯一字符最后一次出现的下标。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "'745138savdsxeynojggpfbkfrt8121'\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n",
      "8\n",
      "9\n",
      "10\n",
      "12\n",
      "13\n",
      "14\n",
      "15\n",
      "16\n",
      "17\n",
      "20\n",
      "22\n",
      "23\n",
      "25\n",
      "26\n",
      "29\n"
     ]
    }
   ],
   "source": [
    "x = input()\n",
    "y = {}\n",
    "for i in x:\n",
    "     y[i] = y.get(i,0)+1\n",
    "for j in y:\n",
    "    if y[j] == 1:\n",
    "        print(x.index(j))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4．输入包含若干集合的列表，输出这些集合的并集。提示:使用`reduce()`函数和`operator`模块中的运算实现多个集合的并集。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{5,8,7},{9,6,7}\n",
      "{5, 6, 7, 8, 9}\n"
     ]
    }
   ],
   "source": [
    "from functools import reduce\n",
    "from operator import __or__\n",
    "s=eval(input())\n",
    "print(reduce(__or__,s))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "5．输入一个字符串,检查该字符串是否为回文（正着读和反看读都一样的字符串)，如果是就输出`Yes`，否则输出`No`。要求使用切片实现"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 课本上65页课后习题"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1.Python3.x的range()函数返回一个___可迭代对象____\n",
    "\n",
    "2.表达式“[3]in[1,2,3,4]”的值为__False____\n",
    "\n",
    "3.列表对象的sort()方法用来对列表元素进行原地排序，该函数返回值为___None_____\n",
    "\n",
    "4.列表对象的__remove()__方法删除首次出现的指定元素，如果列表中不存在要删除的元素，则抛出异常。\n",
    "\n",
    "5.假设列表对象aList的值为[3,4,5,6,7,9,11,13,15,17]，那么切片aList[3:7]得到的值是___[6,7,9,11]______\n",
    "\n",
    "6.在Python中，字典和集合都是用一对___大括号___作为界定符，字典的每个元素由两部分组成，即__“键”和“值”,其中“键”__不允许重复。\n",
    "\n",
    "7.使用字典对象的__items()__方法可以返回字典的“键：值”对，使用字典对象的__keys()__方法可以返回字典的“键”，\n",
    "使用字典对象的___values()___方法可以返回字典的“值”。\n",
    "\n",
    "8.假设有列表a=['name','age','sex']和b=['Dong',38,'Male'],使用一条语句将这两个列表的内容转换为字典，并且以列表a中的元素为“键”，\n",
    "以列表b中的元素为“值”，这个语句可以写为__c=dict(zip(a,b))_________\n",
    "\n",
    "9.假设有一个列表a，现要求从列表a中每三个元素取一个，并且将取到的元素组成新的列表b，可以使用语句___b=a[::3]_______\n",
    "\n",
    "10.使用列表推导式生成包含10个数字5的列表，语句可以写为_[5 for i in range(10)]________。\n",
    "\n",
    "11.不可以（可以，不可以）使用del命令来删除元组中的部分元素。\n",
    "\n",
    "12.已知列表data中有若干字符串，要求编写程序，对data中的字符串进行过滤，只输出重复字符不超过一半的字符串。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['cesf8', 'sf7saf', '841351', '841351', 'd7f866?']\n"
     ]
    }
   ],
   "source": [
    "from collections import Counter\n",
    "data=['cesf8','sf7saf','841351','841351','d7f866?']\n",
    "for d in data:\n",
    "    res=Counter(d)\n",
    "    max_len=0\n",
    "    for key in res:\n",
    "        if max_len<res[key]:\n",
    "            max_len=res[key]\n",
    "    n=len(d)            \n",
    "    if max_len>n/2:\n",
    "        data.remove(d)\n",
    "print(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def ab(x):\n",
    "    for i in range(2,x):\n",
    "        if x%i==0:\n",
    "            return False\n",
    "        if i==x-1:\n",
    "            return True\n",
    "cd = filter(ab,range(2,1001))\n",
    "print(list(cd))#13"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
